package jvs.vfs.tools;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;

public class GZip {
      public static void compress(File infile, File outfile) throws IOException {
            BufferedInputStream in = new BufferedInputStream(
            new FileInputStream(infile));
            BufferedOutputStream out = new BufferedOutputStream(
                  new GZIPOutputStream(new FileOutputStream(outfile)));
            int c;
            byte[] buf = new byte[1024];
            while((c = in.read(buf)) != -1)  {
                out.write(buf, 0, c);
            }
            in.close();
            out.close();
            
            // now decompress our new file
          //  BufferedReader in2 = new BufferedReader( new InputStreamReader(new GZIPInputStream(new FileInputStream("out.gz"))));
          //  String s;
          //  while ((s = in2.readLine()) != null) {
          //        System.out.println(s);
         //   }
      }
      
      public static void main(String[] args) {
    	  File in = new File("c:/liqiang/pic/ivanka.trump.jpg");
    	  File out = new File("c:/liqiang/pic/img.gz");
    	  try {
			compress(in,out);
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	  System.out.println("done. "+in.length()+">"+out.length());
      }
}